//
//  UrbanLogInViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/8.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanLogInViewController.h"
#import "UrbanLogInTextFild.h"
#import "UrbanRegistViewController.h"
#import "UrbanForgetPasswordViewController.h"

@interface UrbanLogInViewController ()
{
    NSString *_phoneNum;
    NSString *_passWord;
}

@end

@implementation UrbanLogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}


-(void)creatView
{
    self.view.backgroundColor = [UIColor whiteColor];

    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(0, 26*screenW_6 + NavBarHeight, 125*screenW_6, 125*screenW_6) backGroundImageV:@"logo"];
    [self.view addSubview:imageV];
    imageV.center = CGPointMake(self.view.frame.size.width/2, imageV.center.y);
    
    //手机号
    UrbanLogInTextFild *phoneTextFild= [[UrbanLogInTextFild alloc]initWithFrame:CGRectMake(30*screenW_6, CGRectGetMaxY(imageV.frame) + 72/2*screenW_6, 0, 0) imageName:@"phone" placeholder:@"手机号" keyboardType:UIKeyboardTypeNumberPad maxNum:11 type:UrbanLogInTextFildPhoneType];
    [self.view addSubview:phoneTextFild];
    phoneTextFild.UrbanLogInTextFildblock = ^(NSString *phone){
        self->_phoneNum = phone;
    };
    
    //密码
    UrbanLogInTextFild *passWordTextFild= [[UrbanLogInTextFild alloc]initWithFrame:CGRectMake(30*screenW_6, CGRectGetMaxY(phoneTextFild.frame) + 5*screenW_6, 0, 0) imageName:@"password" placeholder:@"密码" keyboardType:UIKeyboardTypeDefault maxNum:12 type:UrbanLogInTextFildPassWordType];
    [self.view addSubview:passWordTextFild];
    passWordTextFild.UrbanLogInTextFildblock = ^(NSString *pasWord){
        self->_passWord = pasWord;
    };
    
    //登陆
    UIButton *loginBtn = [TomamaUI createButton:0 Frame:CGRectMake(15*screenW_6, CGRectGetMaxY(passWordTextFild.frame) + 45*screenW_6, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(logion) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16*screenW_6] title:@"登录" image:nil backGroundImage:@"blue_button2"];
    loginBtn.layer.cornerRadius = 3*screenW_6;
    loginBtn.layer.masksToBounds = YES;
    [self.view addSubview:loginBtn];
    
//    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(loginBtn.frame) + 25*screenW_6, 1, 14*screenW_6)];
//    lineView.backgroundColor = Color(204, 204, 204);
//    [self.view addSubview:lineView];
//    lineView.center = CGPointMake(self.view.frame.size.width/2, lineView.center.y);
//
//    //忘记密码
//    NSString *forget = @"忘记密码";
//    UIButton *forgetBtn = [TomamaUI createButton:0 Frame:CGRectMake(lineView.frame.origin.x - [TomamaUI getStringWeithtWith:forget andHeigth:14*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]] - 16*screenW_6, 0, [TomamaUI getStringWeithtWith:forget andHeigth:14*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]], 30*screenW_6) targ:self sel:@selector(forgetPassWord) titleColor:Color(153, 153, 153) font:[UIFont systemFontOfSize:14*screenW_6] title:forget image:nil backGroundImage:nil];
//    [self.view addSubview:forgetBtn];
//    forgetBtn.center = CGPointMake(forgetBtn.center.x, lineView.center.y);
//
//    //用户注册
//    NSString *regist = @"立即注册";
//    UIButton *registBtn = [TomamaUI createButton:0 Frame:CGRectMake(lineView.frame.origin.x + 16*screenW_6, 0, [TomamaUI getStringWeithtWith:regist andHeigth:14*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]], 30*screenW_6) targ:self sel:@selector(regist) titleColor:Color(153, 153, 153) font:[UIFont systemFontOfSize:14*screenW_6] title:regist image:nil backGroundImage:nil];
//    [self.view addSubview:registBtn];
//    registBtn.center = CGPointMake(registBtn.center.x, lineView.center.y);
//
//    //第三方登录
//    NSString *weixinStr = @"第三方帐号登录";
//    UILabel *lableWx = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [TomamaUI getStringWeithtWith:weixinStr andHeigth:14*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]], 14*screenW_6)];
//    lableWx.text = weixinStr;
//    lableWx.textColor = Color(153, 153, 153);
//    lableWx.font = [UIFont systemFontOfSize:14*screenW_6];
//    [self.view addSubview:lableWx];
//    lableWx.center = CGPointMake(SCREENW/2, registBtn.frame.origin.y + 107*screenW_6);
//
//    UIView *leftLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25*screenW_6, 1)];
//    leftLineView.backgroundColor = Color(204, 204, 204);
//    [self.view addSubview:leftLineView];
//    leftLineView.center = CGPointMake(SCREENW/2 - lableWx.frame.size.width/2 - 7*screenW_6 - 25/2*screenW_6, registBtn.frame.origin.y + 107*screenW_6);
//
//    UIView *rightLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25*screenW_6, 1)];
//    rightLineView.backgroundColor = Color(204, 204, 204);
//    [self.view addSubview:rightLineView];
//    rightLineView.center = CGPointMake(SCREENW/2 + lableWx.frame.size.width/2 + 7*screenW_6 + 25/2*screenW_6, registBtn.frame.origin.y + 107*screenW_6);
//
//    //微信
//    UIButton *weixinBtn = [TomamaUI createButton:0 Frame:CGRectMake(0, 0, 75/2*screenW_6, 75/2*screenW_6) targ:self sel:@selector(goWinXin) titleColor:nil font:nil title:nil image:@"weixing" backGroundImage:nil];
//    [self.view addSubview:weixinBtn];
//    weixinBtn.center = CGPointMake(SCREENW/2, lableWx.frame.origin.y + 29*screenW_6 + weixinBtn.frame.size.height/2);
}

#pragma mark - @selector

-(void)logion
{
    [TomamaUI monitorNetworking:^(bool type) {
        if (type) {
            [HudHelper showProgressHudWithMessage:@"" toView:self.view];
            self->_passWord = [ToolViewVC getNotKongGeStr:self->_passWord];
            if (self->_phoneNum.length!=11) {
                [HudHelper hideHudToView:self.view];
                [TomamaUI creatAlertWithTarget:self message:@"请正确填写手机号" timeInterval:2];
                return;
            }
            if ( self->_passWord.length<6) {
                [HudHelper hideHudToView:self.view];
                [TomamaUI creatAlertWithTarget:self message:@"密码不能小于6位" timeInterval:2];
                return;
            }
            NSMutableDictionary *dic = [NSMutableDictionary new];
            [dic setValue:self->_phoneNum forKey:@"mobile"];
            [dic setValue:self->_passWord forKey:@"password"];
            
            [HttpRequest PostrequestUrl:@"jeesite/appInterface/loginInterface/login" Dic:dic completion:^(NSDictionary *responseDic)
             {
                 [HudHelper hideHudToView:self.view];
                 
//                 NSLog(@"respond:%@",responseDic);
                 if ([responseDic[@"success"] boolValue]) {//请求成功
                     
                     NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:responseDic[@"user"]];
                     [standfault setObject:dic forKey:@"userInfo"];
                     [standfault synchronize];
                     appDelegate.window.rootViewController = [RWBTabBarViewController new];
//                     [Storage isInJiguang];//注册极光
                 }else//请求失败提示
                 {
                     [ToolViewVC addKeyAlterViewController:responseDic[@"msg"]?responseDic[@"msg"]:@"登录失败" Controller:self];
                 }
             }];
        }else{
            [TomamaUI creatAlertWithTarget:self message:@"请检查网络" timeInterval:2.0];
            return ;
        }
    }];
    
}

-(void)forgetPassWord//忘记密码
{
    [self.navigationController pushViewController:[UrbanForgetPasswordViewController new] animated:YES];
}

-(void)regist//注册
{
    [self.navigationController pushViewController:[UrbanRegistViewController new] animated:YES];
}

-(void)goWinXin//微信
{
    NSLog(@"三方登录");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
